package com.danskin.wechat.web;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.api.client.WeiXinOpenApiClient;
import com.danskin.wechat.entity.RelFansTag;
import com.danskin.wechat.entity.SceneQrcode;
import com.danskin.wechat.entity.SceneQrcodeStatisView;
import com.danskin.wechat.entity.WxCardConfig;
import com.danskin.wechat.entity.WxFansInfoView;
import com.danskin.wechat.entity.WxFansMsgReply;
import com.danskin.wechat.entity.WxFansMsgView;
import com.danskin.wechat.entity.WxMenuInfo;
import com.danskin.wechat.entity.WxMessage;
import com.danskin.wechat.entity.WxMessageNews;
import com.danskin.wechat.entity.WxMsgReplyDetail;
import com.danskin.wechat.entity.WxTagInfo;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.WxCardConfigService;
import com.danskin.wechat.service.WxMessageService;
import com.danskin.wechat.service.WxTagInfoService;
import com.danskin.wechat.util.FTPHelper;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.util.StringUtil;

/**
 * @author Allen Liu
 */
@Controller
@RequestMapping("weixin")
public class WeiXinController extends BaseController {

	@Autowired
	private WxTagInfoService wxTagInfoService;

	@Autowired
	private WxMessageService wxMessageService;
	
	@Autowired
    private WxCardConfigService wxCardConfigService;

	@RequestMapping(value = "msgReply", method = RequestMethod.GET)
	public String msgReply(HttpServletRequest request, Model model) {

		return "weixin/msgReply";
	}

	@RequestMapping(value = "msgReplyEdit", method = RequestMethod.GET)
	public String msgReplyEdit(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id,
			Model model) {

		WxMsgReplyDetail msgReply = new WxMsgReplyDetail();
		if (id != null) {
			msgReply = wechatOpService.queryMsgReplyDetail(id, this.getMpId());
		} else {
			msgReply.setId(0);
		}
		model.addAttribute("msgReply", msgReply);

		return "weixin/msgReplyEdit";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getReplyMsg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object getReplyMsg(Page page, HttpServletRequest request) throws Exception {

		Map<String, Object> map = getRequestParameter(request);
		map.put("mpId", this.getMpId());
		page.setMap(map);

		List<WxMsgReplyDetail> list = wechatOpService.findReplyDetailList(page);
		return pagelistToJSONForBootStrapTable(list, page);
	}

	@RequestMapping(value = "deleteReplyMsg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object deleteReplyMsg(@RequestParam(value = "id", required = true) Integer id, HttpServletRequest request)
			throws Exception {

		int result = wechatOpService.deleteMsgReplyDetail(id, this.getMpId());

		return new AccessResponse<String>(result > 0 ? "0" : "-1", "");
	}

	@RequestMapping(value = "saveReplyMsg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object saveReplyMsg(WxMsgReplyDetail msgReply, HttpServletRequest request) throws Exception {

		msgReply.setMpId(this.getMpId());

		wechatOpService.saveMsgReplyDetail(msgReply);

		return new AccessResponse<String>("0", "");
	}

	@ResponseBody
	@RequestMapping(value = "fileUpload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object fileUpload(@RequestParam("fileIcon") MultipartFile image) throws IllegalStateException, IOException {
		try {
			String filetemp = System.getProperty("catalina.home") + File.separator + "danskin-temp";

			File filetempDir = new File(filetemp);
			if (!filetempDir.exists()) {
				filetempDir.mkdirs();
			}
			File imageFile = new File(filetemp + File.separator + image.getOriginalFilename());
			image.transferTo(imageFile);

			String accessToken = this.getMPAccessToken();
			return WeiXinOpenApiClient.createInstance().addMaterial(accessToken, "image", "image/jpeg", imageFile,
					null);

		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;
	}

	@ResponseBody
	@RequestMapping(value = "fileUpload2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object fileUpload2(@RequestParam("filePicUrl1") MultipartFile file, @RequestParam("folder") String folder)
			throws IllegalStateException, IOException {
		try {

			String pictureUrl = FTPHelper.getInstance().uploadPictureAndReturnPictureUrl(file, folder,
					FTPHelper.uploadTemporaryFile());

			return new AccessResponse<String>("0", "ok", pictureUrl);

		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;
	}

	@ResponseBody
	@RequestMapping(value = "getMaterial", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getMaterial(@RequestParam(value = "pageIndex", required = true) int pageIndex) {
		try {
			int pageSize = 20;
			int offset = (pageIndex - 1) * pageSize;

			String accessToken = this.getMPAccessToken();
			return WeiXinOpenApiClient.createInstance().getMaterialBatch(accessToken, "news", offset, pageSize);

		} catch (Exception e) {
			e.printStackTrace();

		}
		return null;
	}

	@RequestMapping(value = "customMenu", method = RequestMethod.GET)
	public String customMenu(HttpServletRequest request, Model model) {

		return "weixin/customMenu";
	}

	@ResponseBody
	@RequestMapping(value = "getAllMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getAllMenu() {

		List<WxMenuInfo> list = wechatOpService.queryAllMenu(this.getMpId());
		return new AccessResponse<List<WxMenuInfo>>("0", "", list);
	}

	@ResponseBody
	@RequestMapping(value = "getMenuInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getMenuInfo(int id) {

		WxMenuInfo menu = wechatOpService.queryMenuInfo(id, this.getMpId());
		return new AccessResponse<WxMenuInfo>("0", "", menu);
	}

	@ResponseBody
	@RequestMapping(value = "saveMenuInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object saveMenuInfo(WxMenuInfo record, HttpServletRequest request) {
		record.setMpId(this.getMpId());
		int result = wechatOpService.saveMenuInfo(record);

		return new AccessResponse<WxMenuInfo>(result > 0 ? "0" : "-1", "", record);
	}

	@ResponseBody
	@RequestMapping(value = "deleteMenuInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object deleteMenuInfo(int id) {

		int result = wechatOpService.deleteMenuInfo(id, this.getMpId());

		return new AccessResponse<Object>(result > 0 ? "0" : "-1", "");
	}

	@ResponseBody
	@RequestMapping(value = "publishMenu", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object publishMenu() {
		int result = 0;
		try {

			String accessToken = this.getMPAccessToken();
			result = wechatOpService.publishWechatMenu(accessToken, this.getMpId());
			// return new AccessResponse<Integer>("0", "", result);
		} catch (Exception e) {
			e.printStackTrace();

		}
		return new AccessResponse<Object>(result > 0 ? "0" : "-1", "");
	}

	@RequestMapping(value = "fansMsg", method = RequestMethod.GET)
	public String fansMsg(HttpServletRequest request, Model model) {

		return "weixin/fansMsg";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "pageFansMsg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object pageFansMsg(Page page, HttpServletRequest request) throws Exception {

		Map<String, Object> map = getRequestParameter(request);
		map.put("mpId", this.getMpId());
		map.put("msgType", "text");
		page.setMap(map);

		List<WxFansMsgView> list = wechatOpService.findFansMsgList(page);
		return pagelistToJSONForBootStrapTable(list, page);
	}

	@ResponseBody
	@RequestMapping(value = "getFansMsg", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object getFansMsg(int id) {
		int mpId = this.getMpId();
		WxFansMsgView entity = wechatOpService.findFansMsgView(id, mpId);
		if (entity != null) {

			List<WxFansMsgReply> msgReplyList = wechatOpService.findFansMsgReplyByMsgId(id, mpId);
			entity.setMsgReplyList(msgReplyList);
		}
		return new AccessResponse<WxFansMsgView>("0", "", entity);
	}

	@ResponseBody
	@RequestMapping(value = "saveFansMsgReply", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object saveFansMsgReply(WxFansMsgReply record, HttpServletRequest request) {

		record.setMpId(this.getMpId());
		record.setKfId(this.getUserId());

		String accessToken = this.getMPAccessToken();
		int result = wechatOpService.kfReplyMsg(accessToken, record);
		String errMsg = "";
		if (result == -1) {
			errMsg = "48小时之后不能回复客服消息。";
		}

		return new AccessResponse<WxFansMsgReply>(result > 0 ? "0" : String.valueOf(result), errMsg, record);
	}

	@RequestMapping(value = "fansList", method = RequestMethod.GET)
	public String fansList(HttpServletRequest request, Model model) {
		List<WxTagInfo> list = wxTagInfoService.getAllWxTagInfo();
		model.addAttribute("list", list);
		return "weixin/fansList";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "pageFansList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object pageFansList(Page page, HttpServletRequest request) throws Exception {

		Map<String, Object> map = getRequestParameter(request);
		map.put("mpId", this.getMpId());
		page.setMap(map);

		List<WxFansInfoView> list = wechatOpService.findFansInfoList(page);
		return pagelistToJSONForBootStrapTable(list, page);
	}

	@ResponseBody
	@RequestMapping(value = "syncFansData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object syncFansData() {

		String accessToken = this.getMPAccessToken();
		wechatOpService.syncFansFromWechat(accessToken, this.getMpId());

		return new AccessResponse<Object>("0", "");
	}

	@RequestMapping(value = "sceneQrcode", method = RequestMethod.GET)
	public String sceneQrcode(HttpServletRequest request, Model model) {

		return "weixin/sceneQrcode";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getSceneQrcode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object getSceneQrcode(Page page, HttpServletRequest request) throws Exception {

		Map<String, Object> map = getRequestParameter(request);
		map.put("mpId", this.getMpId());
		map.put("type", 1);
		page.setMap(map);

		List<SceneQrcode> list = wechatOpService.findSceneQrcodeList(page);
		return pagelistToJSONForBootStrapTable(list, page);
	}

	@RequestMapping(value = "sceneQrcodeEdit", method = RequestMethod.GET)
	public String sceneQrcodeEdit(HttpServletRequest request, @RequestParam(value = "id", required = false) Integer id,
			Model model) {

		WxMessage message = null;
		SceneQrcode entity = null;
		List<WxMessageNews> newsList = new ArrayList<>();
		if (id != null) {
			entity = wechatOpService.querySceneQrcode(id, this.getMpId());
			if (entity.getMessageId() != null && entity.getMessageId() > 0) {
				message = wxMessageService.getWxMessage(entity.getMessageId());
				if (message != null && message.getMsgType().equals(4)) {// 获取自定义图文消息明细
					newsList = wxMessageService.getWxMessageNews(message.getId());
				}
			}
		} else {
			entity = new SceneQrcode();
			entity.setId(0);
			entity.setLimitType(0);
			entity.setMessageId(0);
		}

		model.addAttribute("sceneQrcode", entity);
		if (message == null) {
			message = new WxMessage();
		}
		model.addAttribute("message", message);
		if (newsList.size() == 0) {
			WxMessageNews news = new WxMessageNews();
			newsList.add(news);
		}
		model.addAttribute("newsList", newsList);
		
		List<WxCardConfig> wxCardConfigList = wxCardConfigService.selectListForGiftConfig(this.getMpId());
		model.addAttribute("wxCardConfigList", wxCardConfigList);

		return "weixin/sceneQrcodeEdit";
	}

	@RequestMapping(value = "saveSceneQrcode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object saveSceneQrcode(@RequestParam Map<String, Object> map, HttpServletRequest request) throws Exception {

		String accessToken = this.getMPAccessToken();

		SceneQrcode qrcode = new SceneQrcode();
		qrcode.setId(Integer.valueOf(map.get("id").toString()));
		qrcode.setName(map.get("name").toString());
		String sLimitType = this.getMapValue("limitType", map);
		if (!StringUtil.isNullOrEmpty(sLimitType)) {
			qrcode.setLimitType(Integer.valueOf(sLimitType));
		}
		String sExpiredTime = this.getMapValue("expiredTime", map);
		if (!StringUtil.isNullOrEmpty(sExpiredTime)) {
			SimpleDateFormat date_sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			qrcode.setExpiredTime(date_sdf.parse(sExpiredTime));
		}

		qrcode.setMessageId(Integer.valueOf(map.get("messageId").toString()));
		qrcode.setMpId(this.getMpId());
		qrcode.setType(1);
		qrcode.setSceneStr(StringUtil.getShortUUID());
		qrcode.setCouponTemplateId(map.get("couponTemplateId").toString());

		WxMessage message = new WxMessage();
		message.setMpId(this.getMpId());
		List<WxMessageNews> newsList = null;
		message.setMsgType(Integer.valueOf(map.get("msgType").toString()));
		if (message.getMsgType().equals(1)) {
			message.setReplyText(map.get("replyText").toString());
		} else if (message.getMsgType().equals(2)) {
			message.setMediaId(map.get("imageMediaId").toString());
		} else if (message.getMsgType().equals(3)) {
			message.setMediaId(map.get("mpnewsMediaId").toString());
		} else if (message.getMsgType().equals(4)) {
			String newsJson = map.get("newsJson").toString();
			if (!StringUtil.isNullOrEmpty(newsJson)) {
				try {

					JSONArray array = JSONObject.parseArray(newsJson);
					if (array != null) {
						newsList = new ArrayList<>();
						for (int i = 0; i < array.size(); i++) {
							JSONObject obj = array.getJSONObject(i);
							WxMessageNews news = new WxMessageNews();
							news.setTitle(obj.getString("newsTitle"));
							news.setDescription(obj.getString("newsDesc"));
							news.setPicUrl(obj.getString("picUrl"));
							news.setLinkUrl(obj.getString("linkUrl"));
							newsList.add(news);
						}
					}

				} catch (Exception ex) {
					LogUtil.error("二维码维护-自定义图文错误：" + ex.getMessage());
				}

			}
		}

		String realPath = request.getSession().getServletContext().getRealPath("/");
		wechatOpService.saveSceneQrcodeWithReply(accessToken, qrcode, realPath, message, newsList);

		return new AccessResponse<Object>("0", "");
	}

	@RequestMapping(value = "deleteSceneQrcode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object deleteSceneQrcode(@RequestParam(value = "id", required = true) Integer id, HttpServletRequest request)
			throws Exception {

		int result = wechatOpService.deleteSceneQrcode(id, this.getMpId());

		return new AccessResponse<String>(result > 0 ? "0" : "-1", "");
	}

	@RequestMapping(value = "sceneQrcodeStatis", method = RequestMethod.GET)
	public String sceneQrcodeStatis(HttpServletRequest request, @RequestParam(value = "id", required = true) Integer id,
			Model model) {

		SceneQrcode entity = wechatOpService.querySceneQrcode(id, this.getMpId());
		model.addAttribute("sceneQrcode", entity);

		return "weixin/sceneQrcodeStatis";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getSceneQrcodeStatis", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object getSceneQrcodeStatis(Page page, HttpServletRequest request) throws Exception {

		Map<String, Object> map = getRequestParameter(request);
		map.put("mpId", this.getMpId());
		// map.put("sqId", 5);
		page.setMap(map);

		List<SceneQrcodeStatisView> list = wechatOpService.findSceneQrcodeStatisList(page);
		return pagelistToJSONForBootStrapTable(list, page);
	}

	/**
	 * 粉丝页面 点击打标签 跳转页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toFansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String toFansTag(Integer id, Model model) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("mpId", this.getMpId());
		param.put("fansId", id);
		List<WxTagInfo> usedFansTag = wxTagInfoService.selectWxTagInfoByFansIdExclude(param);// 查询当前人物
																								// 已经打过的标签
																								// 过滤掉
		model.addAttribute("tag", usedFansTag);
		model.addAttribute("id", id);
		return "weixin/tag";
	}

	/**
	 * 管理粉丝现有标签 页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toManageTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String toManageTag(Integer id, Model model) throws Exception {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("mpId", this.getMpId());
		param.put("fansId", id);
		List<WxTagInfo> usedFansTag = wxTagInfoService.getUsedFansTag(param);// 查询当前人物
																				// 已经打过的标签
																				// 过滤掉
		model.addAttribute("tag", usedFansTag);
		model.addAttribute("id", id);
		return "weixin/manageTag";
	}

	/**
	 * 给粉丝打标签
	 * 
	 * @param userId
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toSaveTagForCust", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String toSaveTagForCust(Integer userId, @RequestParam(name = "tagId[]") Integer[] tagId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<RelFansTag> list = new ArrayList<RelFansTag>();
		if (tagId != null) {
			for (int i = 0; i < tagId.length; i++) {
				RelFansTag relFansTag = new RelFansTag();
				relFansTag.setMpId(this.getMpId());
				relFansTag.setTagId(tagId[i]);
				relFansTag.setFansId(userId);
				relFansTag.setCreatedTime(new Date());
				list.add(relFansTag);
			}
			int i = wxTagInfoService.insertRelFansTagBatch(list);
			if (i > 0) {
				map.put("status", 200);
				map.put("msg", "添加成功");
			}
		} else {
			map.put("status", 400);
			map.put("msg", "请选择标签");
		}

		return JsonUtils.objectToJson(map);
	}

	/**
	 * 给粉丝打标签
	 * 
	 * @param userId
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toDeleteTagForCust", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String toDeleteTagForCust(Integer userId, @RequestParam(name = "tagId[]") Integer[] tagId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<RelFansTag> list = new ArrayList<RelFansTag>();
		if (tagId != null) {
			for (int i = 0; i < tagId.length; i++) {
				RelFansTag relFansTag = new RelFansTag();
				relFansTag.setMpId(this.getMpId());
				relFansTag.setTagId(tagId[i]);
				relFansTag.setFansId(userId);
				list.add(relFansTag);
			}
			int i = wxTagInfoService.deleteRelFansTagBatch(list);
			if (i > 0) {
				map.put("status", 200);
				map.put("msg", "添加成功");
			}
		} else {
			map.put("status", 400);
			map.put("msg", "请选择标签");
		}

		return JsonUtils.objectToJson(map);
	}

	/**
	 * 跳转到粉丝列表 管理页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/fansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String fansTag() throws Exception {

		return "weixin/tag_list";
	}

	/**
	 * 填充 标签列表的数据
	 * 
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/showWxTagList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public Object showWxTagList(Page page) throws Exception {
		Map map = getRequestParameter(getRequest());
		page.setMap(map);
		List<WxTagInfo> list = wxTagInfoService.listPageWxTagInfo(page);
		return this.pagelistToJSONForBootStrapTable(list, page);
	}

	/**
	 * 跳转到粉丝修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toEditFansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String toEditFansTag(Integer id, String tagName, Integer tagId, Model model) throws Exception {
		tagName = new String(tagName.getBytes("iso-8859-1"), "UTF-8");
		model.addAttribute("id", id);
		model.addAttribute("tagName", tagName);
		model.addAttribute("tagId", tagId);
		return "weixin/tag_update";
	}

	/**
	 * 跳到标签添加界面
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toAddFansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String toAddFansTag() throws Exception {
		return "weixin/tag_update";
	}

	/**
	 * 更新粉丝标签
	 * 
	 * @param id
	 * @param tagName
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateWxFansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String updateWxFansTag(Integer id, String tagName, Integer tagId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String token = this.getMPAccessToken();
		int i = wxTagInfoService.updateWxTagInfo(id, tagName, token, tagId);
		if (i > 0) {
			map.put("status", 200);
			map.put("msg", "添加成功");
		}

		return JsonUtils.objectToJson(map);
	}

	/**
	 * 增加粉丝标签
	 * 
	 * @param tagName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addWxFansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String updateWxFansTag(String tagName) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String token = this.getMPAccessToken();
		int i = wxTagInfoService.saveWxTagInfo(tagName, token);
		if (i > 0) {
			map.put("status", 200);
			map.put("msg", "添加成功");
		}

		return JsonUtils.objectToJson(map);
	}

	/**
	 * 删除粉丝标签
	 * 
	 * @param id
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteWxFansTag", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public String deleteWxFansTag(Integer id, Integer tagId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String token = this.getMPAccessToken();
		int i = wxTagInfoService.deleteWxTagInfo(id, tagId, token);
		if (i > 0) {
			map.put("status", 200);
			map.put("msg", "删除成功");
		}

		return JsonUtils.objectToJson(map);
	}

}
